<?php
/**
* This file holds a class for Application_Model_BusStopMapper.
* @package busmap
*/

/**
* This class holds Application_Model_BusStopMapper class, that maps BusStopMapper objects to the database entries.
* @package busmap
*/
class Application_Model_BusStopMapper
{
	protected $_dbTable;
 
  	/**
	* Function that sets the database table for the BusStop objects.
	*/
	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}
 
   	/**
	* Function that gets the database table for the BusStop objects.
	*/
	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_BusStop');
		}
		return $this->_dbTable;
	}
 
	/**
	* Function that saves the given BusStop object to database.
	* If the object's id is set, then a corresponding entry will be updated.
	* Otherwise a new entry will be inserted.
	*/
	public function save(Application_Model_BusStop $busstop)
	{
		$data = array(
			'name'   => $busstop->getName(),
			'coordinates' => new Zend_Db_Expr($this->parseCoordinatesFromObjectToSql($busstop->getCoordinates())),
			'delete_time' => $busstop->getDeleteTime()
		);
 
		if (null === ($id = $busstop->getId())) {
			unset($data['id']);
			$this->getDbTable()->insert($data);
		} else {
			$this->getDbTable()->update($data, array('id = ?' => $id));
		}
	}

	/**
	* Function that finds a BusStop object with the name given.
	* Only a corresponding entry with delete_time null will be returned.
	* Given reference object will be used to hold the data.
	*/
	public function findByName($name, Application_Model_BusStop $busstop){
		$sql = $this->getDbTable()->select()->from($this->getDbTable(), array('*', 'asText(coordinates) as coordinates', 'X(coordinates) as xCoordinate', 'Y(coordinates) as yCoordinate'))->where('name = ?', $name);
		$result = $this->getDbTable()->fetchAll($sql);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		if(!$row->delete_time){
			$busstop->setId($row->id)
					  ->setName($row->name)
					  ->setXCoordinate($row->xCoordinate)
					  ->setYCoordinate($row->yCoordinate)
					  ->setDeleteTime($row->delete_time);
		}
	}
 
	/**
	* Function that finds a BusStop object with the id given.
	* Only a corresponding entry with delete_time null will be returned.
	* Given reference object will be used to hold the data.
	*/
	public function find($id, Application_Model_BusStop $busstop)
	{
		$sql = $this->getDbTable()->select()->from($this->getDbTable(), array('*', 'asText(coordinates) as coordinates', 'X(coordinates) as xCoordinate', 'Y(coordinates) as yCoordinate'))->where('id = ?', $id);
		$result = $this->getDbTable()->fetchAll($sql);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		if(!$row->delete_time){
			$busstop->setId($row->id)
					  ->setName($row->name)
					  ->setXCoordinate($row->xCoordinate)
					  ->setYCoordinate($row->yCoordinate)
					  ->setDeleteTime($row->delete_time);
		}
	}
 
	/**
	* Function that finds all BusStop entries that have the delete_time null.
	* Returns an array of the objects.
	*/
	public function fetchAll()
	{
		$sql = $this->getDbTable()->select()->from($this->getDbTable(), array('*', 'asText(coordinates) as coordinates', 'X(coordinates) as xCoordinate', 'Y(coordinates) as yCoordinate'))->order('name ASC');
		$resultSet = $this->getDbTable()->fetchAll($sql);
		$entries   = array();

		foreach ($resultSet as $row) {
			$entry = new Application_Model_BusStop();
			if(!$row->delete_time){
				$entry->setId($row->id)
					  ->setName($row->name)
					  ->setXCoordinate($row->xCoordinate)
					  ->setYCoordinate($row->yCoordinate);
				$entries[] = $entry;
			}
		}
		return $entries;
	}

	/**
	* Function that generates a SQL sentance to map Coordinates object to the SQL spatial Point value.
	*/
	public function parseCoordinatesFromObjectToSql($coordinates){
		$returnValue = "GeomFromText( 'POINT(".$coordinates->x." ".$coordinates->y.")' )";
		return $returnValue;
	}

}